package rs.udd.model;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

@MappedSuperclass
public abstract class AbstractUser extends AbstractEntity {

	private static final long serialVersionUID = -2979024856294111288L;

	@NotBlank
	@Column(unique = true)
	private String username;

	@NotBlank
	private String password;

	@NotBlank
	private String firstname;

	@NotBlank
	private String lastname;

	@NotNull
	private Boolean active;

	@Email
	@NotBlank
	// @Column(unique = true) // TODO uncomment when project is finished
	private String email;

	@NotNull
	private Boolean emailActive;

	public AbstractUser() {

	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Boolean getEmailActive() {
		return emailActive;
	}

	public void setEmailActive(Boolean emailActive) {
		this.emailActive = emailActive;
	}

	@Override
	public int hashCode() {

		return new HashCodeBuilder()
				.appendSuper(super.hashCode())
				.append(username)
				.append(firstname)
				.append(lastname)
				.append(active)
				.hashCode();
	}

	@Override
	public boolean equals(Object obj) {

		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}

		final AbstractUser otherUser = (AbstractUser) obj;

		return new EqualsBuilder()
				.appendSuper(super.equals(obj))
				.append(username, otherUser.getUsername())
				.append(firstname, otherUser.getFirstname())
				.append(lastname, otherUser.getLastname())
				.append(active, otherUser.getActive())
				.isEquals();
	}

	@Override
	public String toString() {

		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
				.append(super.toString())
				.append("username", username)
				.append("firstname", firstname)
				.append("lastname", lastname)
				.append("active", active)
				.toString();
	}

}
